local zhuluan = fk.CreateSkill {
  name = "lingling__zhuluan",
}

Fk:loadTranslationTable{
  ["lingling__zhuluan"] = "株乱",
  [":lingling__zhuluan"] = "攻击范围内有你的角色回合结束时，你可以获得其一张牌/视为对其使用【杀】，然后其视为对你使用【穷寇勿迫】，"..
  "若你因此交给其牌/受到伤害，你执行另一项。",

  ["lingling__zhuluan_prey"] = "获得%dest一张牌，其视为对你使用【穷寇勿迫】，若交出牌则视为对其使用【杀】",
  ["lingling__zhuluan_slash"] = "视为对%dest使用【杀】，其视为对你使用【穷寇勿迫】，若受到伤害则获得其一张牌",
}

zhuluan:addEffect(fk.TurnEnd, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return target ~= player and player:hasSkill(zhuluan.name) and
      target:inMyAttackRange(player) and not target.dead and
      (not target:isNude() or player:canUseTo(Fk:cloneCard("slash"), target, { bypass_distances = true, bypass_times = true }))
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local all_choices = {
      "lingling__zhuluan_prey::"..target.id,
      "lingling__zhuluan_slash::"..target.id,
      "Cancel",
    }
    local choices = table.simpleClone(all_choices)
    if not player:canUseTo(Fk:cloneCard("slash"), target, { bypass_distances = true, bypass_times = true }) then
      table.remove(choices, 2)
    end
    if target:isNude() then
      table.insert(choices, 1)
    end
    table.insert(choices, "Cancel")
    local choice = room:askToChoice(player, {
      choices = choices,
      skill_name = zhuluan.name,
      all_choices = all_choices,
    })
    if choice ~= "Cancel" then
      event:setCostData(self, {choice = choice})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choice = event:getCostData(self).choice
    if choice:startsWith("lingling__zhuluan_prey") then
      local card = room:askToChooseCard(player, {
        target = target,
        flag = "he",
        skill_name = zhuluan.name,
      })
      room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonPrey, zhuluan.name, nil, false, player)
    else
      room:useVirtualCard("slash", nil, player, target, zhuluan.name, true)
    end
    if player.dead or target.dead or
      not target:canUseTo(Fk:cloneCard("ling__stop_chasing"), player) then return end
    local use = room:useVirtualCard("ling__stop_chasing", nil, target, player, zhuluan.name, true)
    if use == nil or player.dead or target.dead then return end
    if choice:startsWith("lingling__zhuluan_prey") then
      if not (use.damageDealt and use.damageDealt[player]) then
        room:useVirtualCard("slash", nil, player, target, zhuluan.name, true)
      end
    else
      if use.damageDealt and use.damageDealt[player] and not target:isNude() then
        local card = room:askToChooseCard(player, {
          target = target,
          flag = "he",
          skill_name = zhuluan.name,
        })
        room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonPrey, zhuluan.name, nil, false, player)
      end
    end
  end,
})

return zhuluan
